package back;

import core.TestData;
import core.User;
import dao.IUser;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Michal Horak
 */
@ManagedBean(name = "user")
@ApplicationScoped
public class UserBean implements Serializable {

    @EJB
    IUser u;
    private User user = null;
    private List<SelectItem> allUsers;
    private List<User> selectedUsers;
    private boolean flag;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public UserBean() {
    }

    public List<User> getAllUsers() {
        return u.findAllUsers();
    }

    public List<SelectItem> getAllUsersItemList() {
        allUsers = new ArrayList<SelectItem>();
        for (Iterator<User> it = u.findAllUsers().iterator(); it.hasNext();) {
            User e = it.next();
            allUsers.add(new SelectItem(e, e.getName() + " " + e.getSurname() + " - " + e.getLogin().getUsername()));
        }
        return allUsers;
    }

    public List<User> getSelectedUsers() {
        return selectedUsers;
    }

    public void setSelectedUsers(List<User> selectedUsers) {
        this.selectedUsers = selectedUsers;
    }

    public User getUser() {
        return user;
    }

    @PostConstruct
    public void init() {
        this.user = TestData.loggedUser;
        this.flag = false;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public User findUser(int username) {
        return u.findUser(username);
    }

    public int findUserId(String username) {
        return u.findUser(username).getId();
    }

    public String saveUser() {

        try {
            u.updateUser(user);
        } catch (RuntimeException ex) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "a", "Error " + ex.getLocalizedMessage());
            FacesContext context = FacesContext.getCurrentInstance();
            UIComponent mybutton = findComponent(context.getViewRoot(), "u_username");
            context.addMessage(mybutton.getClientId(context), message);

            return "index";
        }

        return "account";
    }

    public String editUser(User user) {
        this.user = user;
        return "user";
    }

    public boolean isStudent() {
        if (user != null) {
            return this.user.isStudent();
        }
        return true;
    }

    private UIComponent findComponent(UIComponent parent, String id) {
        if (id.equals(parent.getId())) {
            return parent;
        }
        Iterator<UIComponent> kids = parent.getFacetsAndChildren();
        while (kids.hasNext()) {
            UIComponent kid = kids.next();
            UIComponent found = findComponent(kid, id);
            if (found != null) {
                return found;
            }
        }
        return null;
    }

    public void saveEdited() {
        if (flag) {
            flag = false;
        } else {
            this.flag = true;
        }
    }
}
